package com.zhixiang.core.util;


import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;


@SuppressWarnings({"unchecked", "rawtypes"})
public class ExportUtil
{
    public static void ExportXls(List<?> paramList, OutputStream paramOutputStream,
                                 String paramString1, String paramString2)
    {
        HSSFWorkbook localHSSFWorkbook = new HSSFWorkbook();
        HSSFSheet localHSSFSheet = localHSSFWorkbook.createSheet("sheet");
        HSSFRow localHSSFRow = localHSSFSheet.createRow(0);
        HSSFCell localHSSFCell = null;
        CellStyle localCellStyle = localHSSFWorkbook.createCellStyle();
        localCellStyle.setAlignment((short)1);
        localCellStyle.setAlignment((short)2);
        org.apache.poi.ss.usermodel.Font localFont = localHSSFWorkbook.createFont();
        localFont.setBoldweight((short)700);
        localCellStyle.setFont(localFont);
        String[] arrayOfString1 = paramString2.split(",");
        for (int i = 0; i < arrayOfString1.length; i++ )
        {
            localHSSFCell = localHSSFRow.createCell(i);
            localHSSFCell.setCellStyle(localCellStyle);
            localHSSFCell.setCellValue(arrayOfString1[i]);
        }
        if ((paramList != null) && (paramList.size() > 0))
        {
            localCellStyle = localHSSFWorkbook.createCellStyle();
            localCellStyle.setAlignment((short)1);
            localCellStyle.setAlignment((short)2);
            localFont = localHSSFWorkbook.createFont();
            localFont.setBoldweight((short)400);
            localCellStyle.setFont(localFont);
            String[] arrayOfString2 = paramString1.split(",");
            // Object localObject1 = null;
            String str = "";
            for (int j = 0; j < paramList.size(); j++ )
            {
                localHSSFRow = localHSSFSheet.createRow((short)j + 1);
                Object localObject2 = paramList.get(j);
                for (int k = 0; k < arrayOfString2.length; k++ )
                {
                    Object obj = getPropertyValue(localObject2, arrayOfString2[k]);
                    if (obj != null)
                        str = obj.toString();
                    else
                        str = "";
                    // str = getValue(arrayOfString2[k], obj);
                    localHSSFCell = localHSSFRow.createCell(k);
                    localHSSFCell.setCellStyle(localCellStyle);
                    localHSSFCell.setCellValue(str);
                    localHSSFSheet.autoSizeColumn((short)k);
                    str = "";
                }
            }
        }
        try
        {
            localHSSFWorkbook.write(paramOutputStream);
        }
        catch (IOException localIOException)
        {
            localIOException.printStackTrace();
        }
    }

    public static void ptExport(List<?> dataList, OutputStream paramOutputStream,
                                String headInfo[], String Attrs[], String sheetName[])
    {
        List<?> paramList = null;
        String paramString2 = null;
        String paramString1 = null;
        HSSFSheet localHSSFSheet = null;
        HSSFRow localHSSFRow = null;
        HSSFCell localHSSFCell = null;
        CellStyle localCellStyle = null;
        String[] arrayOfString1 = null;
        String[] arrayOfString2 = null;
        // Object localObject1 = null;
        Object localObject2 = null;
        Object obj = null;
        String str = "";
        org.apache.poi.ss.usermodel.Font localFont = null;
        HSSFWorkbook localHSSFWorkbook = new HSSFWorkbook();
        for (int ii = 0; ii < sheetName.length; ii++ )
        {

            localHSSFSheet = localHSSFWorkbook.createSheet(sheetName[ii]);
            localHSSFRow = localHSSFSheet.createRow(0);

            localCellStyle = localHSSFWorkbook.createCellStyle();
            localCellStyle.setAlignment((short)1);
            localCellStyle.setAlignment((short)2);
            localFont = localHSSFWorkbook.createFont();
            localFont.setBoldweight((short)700);
            localCellStyle.setFont(localFont);

            paramString2 = headInfo[ii];

            arrayOfString1 = paramString2.split(",");
            for (int i = 0; i < arrayOfString1.length; i++ )
            {
                localHSSFCell = localHSSFRow.createCell(i);
                localHSSFCell.setCellStyle(localCellStyle);
                localHSSFCell.setCellValue(arrayOfString1[i]);
            }
            if (dataList != null && dataList.size() > 0)
            {

                paramList = (List<?>)dataList.get(ii);

            }

            if ((paramList != null) && (paramList.size() > 0))
            {
                paramString1 = Attrs[ii];
                localCellStyle = localHSSFWorkbook.createCellStyle();
                localCellStyle.setAlignment((short)1);
                localCellStyle.setAlignment((short)2);
                localFont = localHSSFWorkbook.createFont();
                localFont.setBoldweight((short)400);
                localCellStyle.setFont(localFont);
                arrayOfString2 = paramString1.split(",");

                for (int j = 0; j < paramList.size(); j++ )
                {
                    localHSSFRow = localHSSFSheet.createRow((short)j + 1);
                    localObject2 = paramList.get(j);
                    for (int k = 0; k < arrayOfString2.length; k++ )
                    {
                        obj = getPropertyValue(localObject2, arrayOfString2[k]);
                        if (obj != null)
                            str = obj.toString();
                        else
                            str = "";
                        // str = getValue(arrayOfString2[k], obj);
                        localHSSFCell = localHSSFRow.createCell(k);
                        localHSSFCell.setCellStyle(localCellStyle);
                        localHSSFCell.setCellValue(str);
                        localHSSFSheet.autoSizeColumn((short)k);
                        str = "";
                    }
                }
            }

        }

        try
        {
            localHSSFWorkbook.write(paramOutputStream);
        }
        catch (IOException localIOException)
        {
            localIOException.printStackTrace();
        }
    }

    public static void ExportPdf(List<Object> paramList, OutputStream paramOutputStream,
                                 String paramString1, String paramString2)
    {
        Document localDocument = new Document();
        try
        {
            PdfWriter.getInstance(localDocument, paramOutputStream);
            localDocument.open();
            BaseFont localBaseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
            com.lowagie.text.Font localFont = new com.lowagie.text.Font(localBaseFont, 8.0F, 1);
            String[] arrayOfString1 = paramString1.split(",");
            String[] arrayOfString2 = paramString2.split(",");
            PdfPTable localPdfPTable = new PdfPTable(arrayOfString1.length);
            localPdfPTable.setWidthPercentage(100.0F);
            localPdfPTable.setHorizontalAlignment(0);
            PdfPCell localPdfPCell = new PdfPCell();
            localPdfPCell.setHorizontalAlignment(1);
            for (int i = 0; i < arrayOfString2.length; i++ )
            {
                localPdfPCell.setPhrase(new Paragraph(arrayOfString2[i], localFont));
                localPdfPTable.addCell(localPdfPCell);
            }
            localFont = new com.lowagie.text.Font(localBaseFont, 8.0F, 0);
            if ((paramList != null) && (paramList.size() > 0))
            {
                // Object localObject1 = null;
                String str = "";
                for (int j = 0; j < paramList.size(); j++ )
                {
                    Object localObject2 = paramList.get(j);
                    for (int k = 0; k < arrayOfString1.length; k++ )
                    {
                        str = getValue(arrayOfString1[k], localObject2);
                        localPdfPCell.setPhrase(new Paragraph(str, localFont));
                        localPdfPTable.addCell(localPdfPCell);
                        str = "";
                    }
                }
            }
            localDocument.add(localPdfPTable);
        }
        catch (DocumentException localDocumentException)
        {
            localDocumentException.printStackTrace();
        }
        catch (IOException localIOException)
        {
            localIOException.printStackTrace();
        }
        finally
        {
            localDocument.close();
        }
    }

    public static String getValue(String paramString, Object paramObject)
    {
        String str1 = "";
        String str2 = "";
        Method localMethod = null;
        try
        {
            if (paramString.indexOf(".") != -1)
            {

                str2 = "get" + paramString.substring(0, 1).toUpperCase()
                       + paramString.substring(1, paramString.length());
                localMethod = paramObject.getClass().getMethod(str2, (Class<?>[])null);
                if (localMethod != null)
                {
                    String str3 = localMethod.getReturnType().getName();
                    if (str3.indexOf("Date") != -1)
                    {
                        SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(
                            "yyyy-MM-dd HH:mm:ss");
                        Object d = localMethod.invoke(paramObject, (Object)null);
                        if (d == null)
                        {
                            str1 = "null";
                        }
                        else
                            str1 = localSimpleDateFormat.format(d);
                    }
                    else
                    {
                        str1 = String.valueOf(localMethod.invoke(paramObject, (Object)null));
                    }
                }
            }
            else
            {
                // str2 = paramString.split(".")[1];
                localMethod = paramObject.getClass().getMethod(str2, (Class<?>[])null);
                if (localMethod != null)
                {
                    String str3 = localMethod.getReturnType().getName();
                    if (str3.indexOf("Date") != -1)
                    {
                        SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(
                            "yyyy-MM-dd HH:mm:ss");
                        Object d = localMethod.invoke(paramObject, (Object)null);
                        if (d == null)
                        {
                            str1 = "null";
                        }
                        else
                            str1 = localSimpleDateFormat.format(d);
                    }
                    else
                    {
                        str1 = String.valueOf(localMethod.invoke(paramObject, (Object)null));
                    }
                }
                str1 = String.valueOf(localMethod.invoke(paramObject, (Object)null));
            }
        }
        catch (SecurityException localSecurityException)
        {
            localSecurityException.printStackTrace();
        }
        catch (NoSuchMethodException localNoSuchMethodException)
        {
            localNoSuchMethodException.printStackTrace();
        }
        catch (InvocationTargetException localInvocationTargetException)
        {
            localInvocationTargetException.printStackTrace();
        }
        catch (IllegalAccessException localIllegalAccessException)
        {
            localIllegalAccessException.printStackTrace();
        }
        return str1;
    }

    public static Object getPropertyValue(Object entity, String PropertyName)
    {
        Object entity1 = entity;
        String[] as = PropertyName.split("[.]");
        if ((as != null) && (as.length > 0))
        {
            for (int j = 0; j < as.length; j++ )
            {
                if (entity1 != null)
                {
                    try
                    {
                        entity1 = invokeMethod(entity1, field2GetMethod(as[j]), new Class[] {});
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                        /*
                         * try { entity1 =invokeMethod(entity1, "get"+as[j], new Class[] {}); }
                         * catch (Exception e1) { e1.printStackTrace(); }
                         */
                    }
                }

            }
        }
        else
        {
            entity1 = null;
        }
        return entity1;
    }

    
    public static Object invokeMethod(Object owner, String methodName, Object[] args)
        throws Exception
    {

        Class ownerClass = owner.getClass();
        if (args == null)
        {
            args = new Object[0];
        }
        Class[] argsClass = new Class[args.length];
        for (int i = 0, j = args.length; i < j; i++ )
        {
            argsClass[i] = args[i].getClass();
        }
        java.lang.reflect.Method method = null;
        try
        {
            method = ownerClass.getMethod(methodName, argsClass);
        }
        catch (Exception e)
        {
            throw e;
        }
        Object object = null;
        if (method != null)
        {
            try
            {
                object = method.invoke(owner, args);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        return object;
    }

    public static String field2GetMethod(String fieldname)
    {
        if (fieldname.substring(1, 2).toUpperCase().equals(fieldname.substring(1, 2)))
        {
            return "get".concat(fieldname);
        }
        else
        {
            return "get".concat(fieldname.substring(0, 1).toUpperCase()).concat(
                fieldname.substring(1));
        }
    }
}
